package com.chuangjiangx.member.coupon.ddd.domain.service;

import com.chuangjiangx.member.score.ddd.application.command.ModifyPayGiftCardRuleCommand;
import com.chuangjiangx.member.basic.ddd.dal.mapper.MbrMerchantDalMapper;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrConfig;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrConfigRepository;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrPayGiftCardRule;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrPayGiftCardRuleRepository;
import com.chuangjiangx.member.coupon.ddd.domain.model.GetChannelEnum;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrPayGiftCardWxStatusEnum;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 会员支付即会员
 *
 * @author wp
 * @date 2018-08-31 16:36
 **/
@Service
@Slf4j
public class MbrPayGiftCardDomainService {

    private final MbrPayGiftCardRuleRepository mbrPayGiftCardRuleRepository;
    private final MbrConfigRepository mbrConfigRepository;
    private final MbrMerchantDalMapper mbrMerchantDalMapper;

    @Autowired
    public MbrPayGiftCardDomainService(MbrPayGiftCardRuleRepository mbrPayGiftCardRuleRepository, MbrConfigRepository mbrConfigRepository,MbrMerchantDalMapper mbrMerchantDalMapper) {
        this.mbrPayGiftCardRuleRepository = mbrPayGiftCardRuleRepository;
        this.mbrConfigRepository = mbrConfigRepository;
        this.mbrMerchantDalMapper = mbrMerchantDalMapper;
    }

    /**
     * 新增支付即会员规则
     * <p>
     * 1，判断商户是否已授权微信公众号
     * 2，判断是否已填写会员卡基础信息
     * --当勾选微信推送时，进行以下判断
     * 3, 判断用户是否开启微信会员卡可同步至微信卡包
     * 4，判断商户是否已填写微信会员卡信息
     * 5，判断微信会员卡审核状态是否通过
     * 6，持久化数据库
     * 7，调用微信接口，创建支付即会员规则
     */
    public void createRule(MbrPayGiftCardRule mbrPayGiftCardRule) {
        //判断是否配置会员卡基础信息
        checkMbrConfig(mbrPayGiftCardRule.getMerchantId());
        //获取商户的微信商户号
        String subMchId = mbrMerchantDalMapper.selectSubMchIdByMerchantId(mbrPayGiftCardRule.getMerchantId().getId());
        Assert.notNull(subMchId, "商户号不存在");
        List<GetChannelEnum> getChannelList = mbrPayGiftCardRule.getGetChannelList();
        if (getChannelList.isEmpty()) {
            throw new IllegalStateException("请至少选择一个领卡渠道");
        }
        //用户未勾选微信推送
        mbrPayGiftCardRuleRepository.save(mbrPayGiftCardRule);
    }


    /**
     * 修改微信支付即会员配置
     *
     * @param command 1,判断商户是否已授权微信公众号
     *                           2,判断用户微信会员卡审核状态
     *                           --当勾选微信推送时，进行以下判断
     *                           3, 判断用户是否开启微信会员卡可同步至微信卡包
     *                           4，判断微信会员卡审核状态是否通过
     *                           5, 更新数据库
     *                           6，判断规则是否已配置
     *                           有--删除后创建
     *                           无--直接创建
     *                           <p>
     *                           --当用户未勾选微信推送时，进行6判断
     *                           7,更新数据库
     *                           有--删除微信平台规则
     *                           无--不做处理
     */
    public void updatePayGiftCard(ModifyPayGiftCardRuleCommand command) {
        MbrPayGiftCardRule mbrPayGiftCardRule = mbrPayGiftCardRuleRepository.findByMerchantId(command.getMerchantId());
        Assert.notNull(mbrPayGiftCardRule, "规则不存在");
        List<GetChannelEnum> getChannelList = GetChannelEnum.fromCodeList(command.getGetChannelList());
        if (getChannelList.isEmpty()) {
            throw new IllegalStateException("请至少选择一个领卡渠道");
        }
        mbrPayGiftCardRule.update("", command.getBeginTime(), command.getEndTime(), command.getLeastCost(), getChannelList, MbrPayGiftCardWxStatusEnum.UNSET);
        mbrPayGiftCardRuleRepository.update(mbrPayGiftCardRule);


    }

    /**
     * 校验商户是否配置会员卡基础信息
     */
    private void checkMbrConfig(MerchantId merchantId) {
        MbrConfig mbrConfig = mbrConfigRepository.findByMerchantId(merchantId);
        Assert.notNull(mbrConfig, "商户未配置会员卡信息");
    }

}
